AddPlayerPostInit(function(inst)
    -- 暴食素材加载，放这里为了避免加载失败。
    local theLoadPrefabs = {
        "quagmire_swampig","quagmire_crate","quagmire_lamp_short","quagmire_merm_cart1","quagmire_swampigelder","quagmire_grill",
        "quagmire_coin1","quagmire_coin2","quagmire_coin3","quagmire_coin4"
    }
    TheSim:LoadPrefabs(theLoadPrefabs)

    if not GLOBAL.TheWorld.ismastersim then
        return
    end
    
    if inst.components.npc_base_lib == nil then
        inst:AddComponent("npc_base_lib")
    end
    if inst.components.npc_everything_data == nil then
        inst:AddComponent("npc_everything_data")
    end
    
    if inst.components.npc_cards_sys == nil then
        inst:AddComponent("npc_cards_sys")
    end
    
    -------------------------------------------------------------------------------------
    --------- 屏蔽绿色护符带来的偷懒制造
        inst:AddTag("npc_tag_no_green_amulet")
        local function ingredientmod_check_task(inst)
            inst:DoTaskInTime(0.1,function()
                if inst.components.builder and inst.components.builder.ingredientmod < 1 then
                        if inst:HasTag("npc_tag_no_green_amulet") then
                            -- inst:RemoveTag("npc_tag_no_green_amulet")
                            -- inst.npc_base_lib:RPC_RemoveTags({"npc_tag_no_green_amulet"})
                            inst.npc_base_lib:RPC_ChangeTags({  
                                Add = {"npc_tag_has_green_amulet"},
                                Remove = {"npc_tag_no_green_amulet"},
                            })
                            inst:PushEvent("refreshcrafting")
                            inst.npc_base_lib:RPC_PushEvent(inst,"refreshcrafting")
                        end
                else    ----------------------------------------------------------------------------------------------
                        if not inst:HasTag("npc_tag_no_green_amulet") then
                            -- inst:AddTag("npc_tag_no_green_amulet")
                            -- inst.npc_base_lib:RPC_AddTags({"npc_tag_no_green_amulet"})
                            inst.npc_base_lib:RPC_ChangeTags({  
                                Add = {"npc_tag_no_green_amulet"},
                                Remove = {"npc_tag_has_green_amulet"},
                            })
                            inst:PushEvent("refreshcrafting")
                            inst.npc_base_lib:RPC_PushEvent(inst,"refreshcrafting")
                        end
                end
            end)
        end
        inst:ListenForEvent("equip",ingredientmod_check_task)
        inst:ListenForEvent("unequip",ingredientmod_check_task)
    -------------------------------------------------------------------------------------
    --- 同步更新成就
        inst:DoTaskInTime(0.1,function()
            inst.npc_everything_data:Achievements_Synchronize_Update_By_Player()            
        end)
    -------------------------------------------------------------------------------------
    --- 钥匙掉落相关
        if TheWorld.npc_everything_data:Get("woodlegs_out_of_cage.task_end") ~= true then
                local TheWorld_entity_death_event_fn = function(_,_table)
                    -----------------------------------------------------------------------------------------------------
                        local woodlegs_tent_inst = TheSim:FindFirstEntityWithTag("npc_item_woodlegs_tent")
                        if woodlegs_tent_inst ~= nil then
                            return
                        end
                    -----------------------------------------------------------------------------------------------------
                    -- print("TheWorld entity_death event",_table,_table.inst)
                    if _table and _table.inst and _table.inst:HasTag("epic") then
                        local boss = _table.inst
                                -- print("info : entity_death",boss)
                        local keys = {"npc_item_woodlegs_cage_key1","npc_item_woodlegs_cage_key2","npc_item_woodlegs_cage_key3"}
                        local key_name = keys[math.random(#keys)]
                        if boss.components.lootdropper and not boss:HasTag("npc_item_woodlegs_cage_key_spawned") then
                            -- local loot = boss.components.lootdropper.loot or {}
                            -- local insert_flag = true
                            -- for i, v in ipairs(loot) do
                            --     if v and  ( v == "npc_item_woodlegs_cage_key1" or v == "npc_item_woodlegs_cage_key2" or v == "npc_item_woodlegs_cage_key3" ) then
                            --         insert_flag = false
                            --         loot[i] = key_name
                            --         break
                            --     end
                            -- end
                            -- if insert_flag == true then
                            --     table.insert(loot,key_name)
                            --     boss.components.lootdropper.loot = loot
                            -- end
                            ----------------------------------------------------------------------
                            --
                                local x,y,z = boss.Transform:GetWorldPosition()
                                local musthavetags = {"npc_item_woodlegs_cage_key"}
                                local canthavetags = {"INLIMBO"}
                                local musthaveoneoftags = nil
                                local surround_keys = TheSim:FindEntities(x,y,z,80,musthavetags, canthavetags, musthaveoneoftags)
                                if #surround_keys == 0 then
                                    boss.components.lootdropper:SpawnLootPrefab(key_name)
                                    boss:AddTag("npc_item_woodlegs_cage_key_spawned")
                                end
                            ----------------------------------------------------------------------
                        end
                    end

                    if TheWorld.npc_everything_data:Get("woodlegs_out_of_cage.task_end") == true and inst.____woodlegs_cage_key_event_fn then
                        print("Remove inst.____woodlegs_cage_key_event_fn")
                        inst:RemoveEventCallback("entity_death_npc_in_container", inst.____woodlegs_cage_key_event_fn, TheWorld)
                        inst.____woodlegs_cage_key_event_fn = nil
                    end

                    -----------------------------------------------------------------------------------------------------
                end
                if inst.____woodlegs_cage_key_event_fn == nil then
                    inst.____woodlegs_cage_key_event_fn = function(src, data) TheWorld_entity_death_event_fn(inst, data) end
                    inst:ListenForEvent("entity_death", inst.____woodlegs_cage_key_event_fn, TheWorld)
                end
        end

    -------------------------------------------------------------------------------------
    --- 特殊护甲测试
        -- inst.components.inventory.ApplyDamage_npc_old = inst.components.inventory.ApplyDamage
        -- inst.components.inventory.ApplyDamage = function(self,damage, attacker, weapon)
        --     print("ApplyDamage",damage,attacker,weapon)
        --     damage = 0
        --     return self:ApplyDamage_npc_old(damage, attacker, weapon)
        -- end
    -------------------------------------------------------------------------------------
end)
